import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/http/api.dart';
import 'package:flutter_demo/other/application.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:flutter_demo/widget/tiny_widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:universal_html/html.dart';

class AcademicInforModule extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _AcademicInforModuleState();
}

class _AcademicInforModuleState extends State<AcademicInforModule> {

  late GlobalKey formKey;

  late TextEditingController graduationTimeController;
  late TextEditingController academicProgramsController;
  late TextEditingController academicLevelController;
  late TextEditingController academicNatureController;
  late TextEditingController schoolController;
  late TextEditingController academicMajorController;
  late TextEditingController academicDegreeController;
  late TextEditingController academicMarkController;

  bool isSubmitSuccess = false;

  @override
  void initState() {
    super.initState();
    formKey = GlobalKey<FormState>();

    graduationTimeController = TextEditingController();
    academicProgramsController = TextEditingController();
    academicLevelController = TextEditingController();
    academicNatureController = TextEditingController();
    schoolController = TextEditingController();
    academicMajorController = TextEditingController();
    academicDegreeController = TextEditingController();
    academicMarkController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: item(),
    );
  }

  Widget item() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        C(30),
        Form(
          key: formKey,
          child: Wrap(
            spacing: 30,
            runSpacing: 20,
            children: [
              formField('学历', 5, academicDegreeController),
              formField('学历水平', 10, academicLevelController),
              formField('学历性质', 15, academicNatureController),
              formField('毕业院校', 20, schoolController),
              formField('毕业时间', 20, graduationTimeController),
              formField('所学专业', 10, academicMajorController),
              formField('证书编号', 10, academicMarkController),
              formField('学历项目', 10, academicProgramsController),
            ],
          ),
        ),
        C(20),
        Container(
          alignment: Alignment.centerRight,
          padding: EdgeInsets.only(
            right: 40,
          ),
          child: BaseButton(
            radius: 4,
            width: 70,
            height: 36,
            withBorder: false,
            withShadow: false,
            centerButton: false,
            enabled: !isSubmitSuccess,
            buttonColor: Colors.blue,
            onTap: () {
              if ((formKey.currentState as FormState).validate())
                validatePassed(context, () {
                  setState(() => isSubmitSuccess = true);
                });
            },
            child: Text(
              '保存',
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
          ),
        ),
        C(30),
        Container(
          width: double.infinity,
          height: 1,
          margin: EdgeInsets.symmetric(horizontal: 32),
          decoration: BoxDecoration(
            color: Color(0xffeeeeee),
          ),
        ),
      ],
    );
  }

  Widget formField(String title, int maxSize,
      TextEditingController controller, {String? input}) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          width: 100,
          height: 40,
          alignment: Alignment.centerRight,
          margin: EdgeInsets.only(
            left: 15,
            right: 10,
          ),
          child: Text(
            title,
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Color(0xff606266),
              fontSize: 14,
            ),
          ),
        ),
        Container(
          width: 220,
          padding: const EdgeInsets.only(
            left: 10,
            right: 15,
          ),
          child: TextFormField(
            controller: controller,
            decoration: InputDecoration(
              hintText: '请填写${input ?? title}',
              hintStyle: const TextStyle(
                fontSize: 14,
                color: Color(0xff606266),
              ),
              border: InputBorder.none,
            ),
            validator: (value) {
              if (value!.trim().isEmpty) {
                return '${input ?? title}不能为空';
              } else if (value.trim().length > maxSize) {
                return '${input ?? title}超出最大长度';
              } else {
                return null;
              }
            },
          ),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.black26, width: 1),
            borderRadius: BorderRadius.circular(5),
          ),
        ),
      ],
    );
  }

  Future<void> validatePassed(BuildContext context,
      void Function() callback) async {
    Response response = await SystemApi.academic_infor(
        Application.currentAccount,
        graduationTimeController.text,
        academicProgramsController.text,
        academicLevelController.text,
        academicNatureController.text,
        schoolController.text,
        academicMajorController.text,
        academicDegreeController.text,
        academicMarkController.text
    );
    var map = json.decode(response.data);
    if (response.statusCode == 200 && map['code'] == '1') {
      Fluttertoast.showToast(msg: map['msg']);
      callback.call();
    } else if (map['code'] != '1') {
      Fluttertoast.showToast(msg: map['msg']);
    } else {
      Fluttertoast.showToast(msg: response.statusCode.toString());
    }
  }
}